/**
 * 文件名：AppActivity.java
 * 全路径：com.mi.android.AppActivity
 */
package com.mi.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

/**
 * 功能：启动页<br>
 * 作者：蒋睿立<br>
 * 时间：2016年6月14日<br>
 * 版本：<br>
 * 
 */
public class AppActivity extends android.app.Activity {

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(this.getClass().getName(), "action:" + intent.getAction());
			if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
				boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
				Log.i("connected", "connected state change to：" + String.valueOf(connected));
				showConnectState(connected);
			}else if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
				Bundle bundle = intent.getExtras();
				String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
	            Log.i("registration", "Registration Id : " + regId);
	            setLoginRegId();
			}else if (JPushInterface.ACTION_STOPPUSH.equals(intent.getAction())) {
				Log.i("stoppush", "stoppush");
			} else if (JPushInterface.ACTION_RESTOREPUSH.equals(intent.getAction())) {
				Log.i("repush", "repush");
			}
		}
	}

	private android.widget.Button connectStateButton;

	private MessageReceiver mMessageReceiver;
	
	
	/**
	 * 启动《小米商城》
	 * 
	 * @param view
	 */
	public void launcher(android.view.View view) {
		Toast.makeText(this, "启动中...", Toast.LENGTH_LONG).show();
		Center.getInstance().launchMiShop(this);
		Center.getInstance().stopMedia();
	}
	

	/**
	 * 切换推送开关的事件
	 * 
	 * @param view
	 */
	public void push(android.view.View view) {
		boolean isStop = JPushInterface.isPushStopped(this);// 当前状态，true表示推送停止中
		setPushState(isStop);// 设置最新状态，true表示打开推送
	}

	private void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(JPushInterface.ACTION_CONNECTION_CHANGE);
		filter.addCategory("com.mi.android");
		registerReceiver(mMessageReceiver, filter);
	}

	/**
	 * 设置推送状态
	 * 
	 * @param run
	 *            true表示开启推送，false表示关闭推送
	 */
	private void setPushState(boolean run) {
		boolean connected = JPushInterface.getConnectionState(this);
		if (run) {
			JPushInterface.resumePush(getApplicationContext());
		} else {
			JPushInterface.stopPush(getApplicationContext());
		}
		showConnectState(connected);
		boolean stop = JPushInterface.isPushStopped(this);
		Log.i("isPushStopped", String.valueOf(stop));
		Log.i("getConnectionState", String.valueOf(connected));
		
	}

	private void showConnectState(boolean connected) {
		if (connected) {
			connectStateButton.setText(getString(R.string.running));
		} else {
			// Toast.makeText(this, "正在尝试连接服务器！！！", Toast.LENGTH_LONG).show();
			connectStateButton.setText(getString(R.string.stopped));
		}
	}

	@Override
	protected void onCreate(android.os.Bundle savedInstanceState) {
		Log.i("onCreate", "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app);
		this.connectStateButton = (android.widget.Button) findViewById(R.id.push_btn);
//		Center.getInstance().initMedia(this);//初始化声音
		registerMessageReceiver();
	}

	@Override
	protected void onDestroy() {
		Log.i("onDestroy", "onDestroy");
		unregisterReceiver(mMessageReceiver);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
		setPushState(true);
		setLoginRegId();
	}


	private void setLoginRegId() {
		String id = JPushInterface.getRegistrationID(this);
		TextView tv = (TextView)findViewById(R.id.registration);
		tv.setText(getString(R.string.registration_text) + id);
	}
}
